<?php

function cable_feed_load($connector)
{
	return true;
}

function cable_feed_unload($connector)
{
	return true;
}

function cable_feed_open(&$connector)
{
	/* plugin opening routine has no caching behavior */
	if ($connector->data[cached])
		return true;

	/* if the feed variable has been set, retrieve the feeds template */
	if ($_REQUEST['feed'])
	{
		/* load template from db */
		$main_template = new CABLEtemplate($connector->config,$connector->db,'cable_feed','feed');
		if ($main_template->error)
			return 'Error retrieving cable_feed.feed template';
	}
	else /* append link rels to head tag */
	{
		$connector->template->AppendInsert('Head','<link rel="alternate" title="{I CABLE-Site.name /} RSS" href="{I CABLE-Base.url /}?feed=rss" type="application/rss+xml" />');
		$connector->template->AppendInsert('Head','<link rel="alternate" title="{I CABLE-Site.name /} Atom" href="{I CABLE-Base.url /}?feed=atom" type="application/atom+xml" />');
		return true;
	}
	
	/* attempt and switch active templates, but restore the original on failure */
	$old_template = $connector->template;
	switch(strtolower(urldecode($_REQUEST['feed'])))
	{
		case 'atom':
			if (($connector->template = $main_template->GetChild('CABLE-Atom')) === false)
			{
				$connector->template = $old_template;
				return 'Error loading Atom template';
			}
			else /* send appropriate headers */
				header('Content-Type: application/atom+xml; charset=utf-8');
			break;
		case 'rss':
			if (($connector->template = $main_template->GetChild('CABLE-RSS')) === false)
			{
				$connector->template = $old_template;
				return 'Error loading RSS template';
			}
			else
				header('Content-Type: application/rss+xml; charset=utf-8');
			break;
		default:
			unset($old_template);
			return true;
	}
	
	/* ditch old template */
	unset($old_template);
	
	if (count($connector->data) < $connector->config['IndexEntries'])
	{
		/* get the first (i.e. earliest) entry */
		if ($connector->config['IndexSort'] == 'ascending')
			list($first_entry) = array_slice($connector->data,0,1);
		else
			list($first_entry) = array_slice($connector->data,-1,1);
	
		$table = "cable_".$connector->config['TablePrefix']."_entries";
		$query = 'SELECT timestamp,author,title,data FROM $table WHERE (timestamp < '.$first_entry[timestamp].') AND (visible=1) ORDER BY timestamp DESC LIMIT '.($connector->config['IndexEntries'] -count($connector->data));
		
		if (($result = mysql_query($query,$connector->db_link)) === false)
			return 'Unable to retrieve additional entries from database';
		
		/* load entry data results into array */
		while ($row = mysql_fetch_assoc($result))
		{
			/* extract serialized entry data */
			$data = unserialize($row[data]);
			unset($row[data]);
			foreach($data as $key => $value)
			{
				if (!isset($entry_array[$key]))
				{
					if (is_string($value))
						$entry_array[$key] = rawurldecode($value);
					else
						$entry_array[$key] = $value;
				}
			}
			
			$connector->data[$row[timestamp]] = array_merge($data,$row);
		}
	}
	
	/* get the last (i.e latest entry) for the last updated date */
	if ($connector->config['IndexSort'] == 'descending')
		list($last_entry) = array_slice($connector->data,0,1);
	else
		list($last_entry) = array_slice($connector->data,-1,1);
		
	/* retrieve the date formats from the template */
	$date_formats = $connector->template->GetValue('CABLE-DateFormat',true);
	if (count($date_formats) == 0)
		$date_formats = array();
	
	/* set the last published date according to available formats */
	foreach($date_formats as $i => $f)
		$connector->template->ReplaceInsert('CABLE-Feed.date.'.$i,date($f,$last_entry[timestamp]));
		
	return true;
}

function cable_feed($connector)
{
	return true;
}

function cable_feed_close($connector)
{
	return true;
}

?>